package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.ReturnReasonService;
import com.tanhua.model.domain.OmsOrderReturnReason;
import com.tanhua.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/returnReason")
public class ReturnReasonController {

    @Autowired
    private ReturnReasonService returnReasonService;

    /**
     * 分页查询退货原因
     *
     * @param pageNum,pageSize
     * @return
     */
    @GetMapping("/list")
    public R listReturnReason(Integer pageNum, Integer pageSize) {
        PageVo pageVo = returnReasonService.listReturnReason(pageNum, pageSize);
        if (pageVo == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return R.success(pageVo);
    }

    /**
     * 修改退货原因启用状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/update/status")
    public R updateStatus(Integer status, Integer[] ids) {
        returnReasonService.updateStatus(status, ids);
        return R.success("操作成功");
    }

    /**
     * 修改退货原因
     *
     * @param id
     * @param reason
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateById(@PathVariable Integer id, @RequestBody OmsOrderReturnReason reason) {
        returnReasonService.updateById(id, reason);
        return R.success("操作成功");
    }

    /**
     * 获取单个退货原因详情信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        OmsOrderReturnReason reason = returnReasonService.getById(id);
        return R.success(reason);
    }

    /**
     * 批量删除退货原因
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R deletes(Integer[] ids) {
        returnReasonService.deletes(ids);
        return R.success("操作成功");
    }


    /**
     * 添加退货原因
     *
     * @param reason
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody OmsOrderReturnReason reason) {
        returnReasonService.create(reason);
        return R.success("操作成功");
    }

}
